import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';

import 'dart:io';

/// @author zdl
/// date 2020/11/16 15:36
/// email zdl328465042@163.com
/// description 图片控件
class AutoImage extends StatefulWidget {
  final String? path;
  final double? width;
  final double? height;
  final Color? color;
  final BoxFit fit;
  final bool useCached;
  final double? radius;
  final double? radiusTL;
  final double? radiusTR;
  final double? radiusBL;
  final double? radiusBR;
  final double? aspectRatio;
  final VoidCallback? onTap;
  final VoidCallback? onLongPress;
  final String imageFolder;
  final bool preview;

  const AutoImage({
    super.key,
    required this.path,
    this.width,
    this.height,
    this.color,
    this.fit = BoxFit.cover,
    this.useCached = true,
    this.radius,
    this.radiusTL,
    this.radiusTR,
    this.radiusBL,
    this.radiusBR,
    this.aspectRatio,
    this.onTap,
    this.onLongPress,
    this.imageFolder = 'assets',
    this.preview = false,
  });

  @override
  _ZImageState createState() => _ZImageState();
}

class _ZImageState extends State<AutoImage> {
  bool isDown = false;

  @override
  Widget build(BuildContext context) {
    var path = widget.path ?? '';
    Widget? image;
    //加载
    if (path.startsWith('http')) {
      //网络图片
      if (widget.useCached) {
        image = CachedNetworkImage(
          imageUrl: path,
          width: widget.width,
          height: widget.height,
          color: widget.color,
          fit: widget.fit,
        );
      } else {
        // image = FadeInImage.assetNetwork(
        //   placeholder: Icons.image,
        //   image: widget.src,
        //   width: widget.width,
        //   height: widget.height,
        //   fit: widget.fit,
        // );
        image = Image.network(
          path,
          width: widget.width,
          height: widget.height,
          fit: widget.fit,
        );
      }
    } else if (path.contains('/')) {
      //本地文件图片
      image = Image.file(
        File(path),
        width: widget.width,
        height: widget.height,
        fit: widget.fit,
      );
    } else if (path.isNotEmpty) {
      //资源文件图片
      image = Image.asset(
        'assets/images/$path.png',
        width: widget.width,
        height: widget.height,
        color: widget.color,
        fit: widget.fit,
      );
    }
    if (null == image) {
      return const SizedBox();
    }
    //裁剪
    if (null != widget.radius || null != widget.radiusTL || null != widget.radiusTR || null != widget.radiusBL || null != widget.radiusBR) {
      if (widget.radius == double.infinity) {
        image = ClipOval(child: image);
      } else {
        image = ClipRRect(
          borderRadius: null != widget.radius
              ? BorderRadius.circular(widget.radius ?? 0)
              : BorderRadius.only(
                  topLeft: Radius.circular(widget.radiusTL ?? 0),
                  topRight: Radius.circular(widget.radiusTR ?? 0),
                  bottomLeft: Radius.circular(widget.radiusBL ?? 0),
                  bottomRight: Radius.circular(widget.radiusBR ?? 0),
                ),
          child: image,
        );
      }
    }
    //固定宽高比
    if (widget.aspectRatio != null) {
      image = AspectRatio(
        aspectRatio: widget.aspectRatio!,
        child: image,
      );
    }
    //点击事件
    if (widget.preview || widget.onTap != null || widget.onLongPress != null) {
      image = InkWell(
        onTap: () {
          if (null != widget.onTap) widget.onTap!.call();
          if (widget.preview) {
            Navigator.of(context, rootNavigator: true).push(
              MaterialPageRoute(builder: (context) => _BigImage(path: path)),
            );
          }
        },
        onLongPress: widget.onLongPress,
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 100),
          foregroundDecoration: BoxDecoration(
            color: isDown ? Colors.white.withOpacity(0.5) : Colors.transparent,
          ),
          child: image,
        ),
        onTapDown: (_) {
          setState(() {
            isDown = true;
          });
        },
        onTapCancel: () {
          setState(() {
            isDown = false;
          });
        },
        onHighlightChanged: (b) {
          setState(() {
            if (!b) isDown = false;
          });
        },
      );
    }
    return image;
  }
}

class _BigImage extends StatelessWidget {
  final String? path;

  const _BigImage({super.key, this.path});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => Navigator.of(context).pop(),
      child: Container(
        color: Colors.black87,
        child: PhotoView(
          // minScale: 0.5,
          maxScale: 4.0,
          imageProvider: NetworkImage(path ?? ''),
        ),
      ),
    );
  }
}
